import Vue from 'vue'
import Router from 'vue-router'
import Login from './views/Login.vue'
import Home from './views/Home.vue'
import Main from './views/Main.vue'

import Alleviation from './views/main/Alleviation.vue'
import User from './views/main/User.vue'
import Volunteer from './views/main/Volunteer.vue'
import Collection from './views/main/Collection.vue'
import Msgboard from './views/main/Msgboard.vue'
import Poor from './views/main/Poor.vue'
import Carousel from './views/main/Carousel.vue'

import Index from './views/home/Index.vue'
import PoorView from './views/home/PoorView.vue'
import PoorInfo from './views/home/PoorInfo.vue'
import AlleviationView from './views/home/AlleviationView.vue'
import AlleviationInfo from './views/home/AlleviationInfo.vue'
import VolunteerView from './views/home/VolunteerView.vue'
import VolunteerInfo from './views/home/VolunteerInfo.vue'
import VolunteerInfo2 from './views/home/VolunteerInfo2.vue'
import UserView from './views/home/UserView.vue'
import MsgboardView from './views/home/MsgboardView.vue'

Vue.use(Router)

var router =  new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login,
    },
    {
      path: '/',
      name: 'home',
      component: Home,
      redirect:'/index',
      children:[
        {
          path:'/index',
          component:Index
        },
        {
          path:'/poorView',
          component:PoorView
        },
        {
          path:'/poorInfo/:id',
          component:PoorInfo
        },
        {
          path:'/alleviationView',
          component:AlleviationView
        },
        {
          path:'/AlleviationInfo/:id',
          component:AlleviationInfo
        },
        {
          path:'/volunteerView',
          component:VolunteerView
        },
        {
          path:'/VolunteerInfo/:id',
          component:VolunteerInfo
        },
        {
          path:'/VolunteerInfo2',
          component:VolunteerInfo2
        },
        {
          path:'/userView',
          component:UserView
        },
        {
          path:'/msgboardView',
          component:MsgboardView
        },
      ]
    },
    {
      path: '/main',
      name: 'main',
      component: Main,
      children:[
        {
          path: '/alleviation',
          name: 'alleviation',
          component: Alleviation,
        },
        {
          path: '/user',
          name: 'user',
          component: User,
        },
        {
          path: '/volunteer',
          name: 'volunteer',
          component: Volunteer,
        },
        {
          path: '/collection',
          name: 'collection',
          component: Collection,
        },
        {
          path: '/msgboard',
          name: 'msgboard',
          component: Msgboard,
        },
        {
          path: '/poor',
          name: 'poor',
          component: Poor,
        },
        {
          path: '/carousel',
          name: 'carousel',
          component: Carousel,
        },
      ]
    },
  ]
})

//全局路由守卫
router.beforeEach((to,form,next)=>{
  window.console.log(`路由守卫触发,拦截${to.path}`)
  if(to.path == '/login') return next()
   //把token存到sessionStorage里边
  const tonkenStr = sessionStorage.getItem('token')
  if(! tonkenStr) return next('/login')
  next()

})
export default router